package com.cloudkinto.controller.storage;


import com.cloudkinto.base.BaseController;
import com.cloudkinto.bo.storage.StorageAreaAddBo;
import com.cloudkinto.bo.storage.StorageAreaQueryBo;
import com.cloudkinto.bo.storage.StorageAreaUpdateBo;
import com.cloudkinto.bo.storage.StorageImportBo;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.storage.StorageAreaService;
import com.cloudkinto.vo.storage.StorageAreaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 库区表 前端控制器
 * </p>
 *
 * @author long
 * @since 2024-11-12
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "库区表模块")
@RequestMapping("/api/admin/storage/area")
public class StorageAreaController extends BaseController {

    private final StorageAreaService storageAreaService;

    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "库区表新增", notes = "库区表新增")
    public SingleResult add(@RequestBody @Validated StorageAreaAddBo req) {
        storageAreaService.add(req, getTenantId());
        return SingleResult.success("新增成功");
    }

    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "库区表修改", notes = "库区表修改")
    public SingleResult update(@RequestBody @Validated StorageAreaUpdateBo req) {
        Long userId = getCurrentUserId();
        storageAreaService.update(req, userId);
        return SingleResult.success("修改成功");
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "库区表根据id删除", notes = "库区表根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", storageAreaService.delete(id, userId));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "库区表根据id查看详情", notes = "库区表根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", storageAreaService.detail(id));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "库区表查询初始化", notes = "库区表查询初始化")
    public SingleResult<Map<String, Object>> pageInit() {
        return storageAreaService.pageInit();
    }

    @UserSessionRequired
    @PostMapping("/listPage")
    @ApiOperation(value = "库区表查询（分页）", notes = "库区表查询（分页）")
    public PageResult<StorageAreaVo> listPage(@RequestBody StorageAreaQueryBo bo) {
        bo.setTenantId(getTenantId());
        bo.setWhId(getWarehouseId());
        return storageAreaService.getListPage(bo);
    }

    @UserSessionRequired
    @PostMapping("import")
    @ApiOperation(value = "库区表导入", notes = "库区表导入")
    public SingleResult importExcel(StorageImportBo bo) {
        return SingleResult.success(storageAreaService.importExcel(bo, getTenantId()));
    }
}

